package net.wofly.sale.signin.service.impl;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.Identities;
import net.wofly.common.util.PageHelper;
import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import net.wofly.sale.signin.domain.SignIn;
import net.wofly.sale.signin.repository.SignInRepository;
import net.wofly.sale.signin.service.ISignInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author siqingwei
 */
@Service("iSignInService")
@Transactional
public class SignInServiceImpl implements ISignInService {

	@Autowired
	SignInRepository signInRepository;

	@Override
	public SignIn save(SignIn entity) {
		return signInRepository.save(entity);
	}

	@Override
	public void delete(SignIn entity) {
		signInRepository.delete(entity);
	}

	@Override
	public void delete(String key) {
		signInRepository.delete(key);
	}

	@Override
	public SignIn findOne(String key) {
		return signInRepository.findOne(key);
	}

	@Override
	public List<SignIn> save(List<SignIn> entityList) {
		return (List<SignIn>) signInRepository.save(entityList);
	}

	@Override
	public ReturnBean<PageWrapper<SignIn>> getSignIn(Integer pageNum, Integer pageSize, final String employeeID, final Long fromTime, final Long toTime) {
		Condition condition = new Condition();
		List<FilterCondition> filterConditionList = new ArrayList<FilterCondition>();
		filterConditionList.add(new FilterCondition(employeeID, "employeeID", ConditionRelate.and.name(), Operation.eq.name(), 1));
		if (fromTime != null) {
			filterConditionList.add(new FilterCondition(String.valueOf(fromTime), "signInTime", ConditionRelate.and.name(), Operation.ge.name(), 2));
		}

		if (toTime != null) {
			filterConditionList.add(new FilterCondition(String.valueOf(toTime), "signInTime", ConditionRelate.and.name(), Operation.le.name(), 3));
		}
		condition.setFilterCondition(filterConditionList);
		// 获得分页对象pageable，并且在pageable中页码是从0开始，设定按照sortType升序排列
		Pageable pageable = new PageRequest(pageNum - 1,pageSize);
		Page<SignIn> page = signInRepository.findAll(condition.getSpecification(), pageable);
		return new ReturnBean<>(ResultCode.SUCCESS, "获取签到记录成功！", PageHelper.toPage(page));
	}

	@Override
	public ReturnBean<String> signIn(SignIn signIn) {
		Long currentTime = System.currentTimeMillis();
		signIn.setModifyDate(currentTime);
		signIn.setSignInTime(currentTime);
		signIn.setSignInID(Identities.uuid2());
		signInRepository.save(signIn);
		return new ReturnBean<>(0, "签到成功！");
	}

	@Override
	public ReturnBean<String> uploadPic(MultipartFile file, String path) {
		LocalDate today = LocalDate.now();
		String targetPath = File.separator+"static"+File.separator+"images"+File.separator+"signin" + File.separator + today.getYear() + File.separator + today.getMonthValue();
		String fileName = Identities.uuid2() + "." +file.getOriginalFilename().split("\\.")[1];
		String relativePath = targetPath + File.separator + fileName;
		String $file = path + relativePath;
		File uploadedFile = new File($file);
		if(uploadedFile == null){
			return new ReturnBean<>(ResultCode.ERROR_SERVICE, "targetFile is not be null");
		}
		File f= new File(uploadedFile.getParent());
		if(!f.exists()){
			f.mkdirs();
		}

		try {
			FileCopyUtils.copy(file.getBytes(), uploadedFile);
		} catch (IOException e) {
			e.printStackTrace();
			return new ReturnBean<>(ResultCode.ERROR_SERVICE,"上传文件出错！");
		}
		return new ReturnBean<>(ResultCode.SUCCESS,"上传文件成功！", relativePath);
	}
}
